package com.zufe.StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void start() {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> studentList = new ArrayList<>();
        while (flag) {
            System.out.println("-------------欢迎来到黑马学生管理系统----------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择：");
            String choice = sc.next();
            switch (choice) {
                case "1" -> addStudent(studentList);
                case "2" -> deleteStudent(studentList);
                case "3" -> updateStudent(studentList);
                case "4" -> queryStudent(studentList);
                case "5" -> flag = false;
                default -> System.out.println("输入有误，请重新输入");
            }
        }
    }

    public static void addStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生id:");
            String id = sc.next();
            if(contains(students, id)){
                System.out.println("学号已存在，请重新输入");
            }else{
                System.out.println("请输入学生姓名:");
                String name = sc.next();
                System.out.println("请输入学生年龄:");
                int age = sc.nextInt();
                System.out.println("请输入学生地址:");
                String address = sc.next();
                students.add(new Student(id, name, age, address));
                System.out.println("添加成功");
                queryStudent(students);
                break;
            }
        }
    }

    public static void deleteStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要删除的学生id:");
            String id = sc.next();
            int index = getIndex(students, id);
            if(index >= 0){
                students.remove(index);
                System.out.println("删除成功");
                queryStudent(students);
                break;
            }else {
                System.out.println("学号不存在，请重新输入");
            }
        }
    }

    public static void updateStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要修改的学生id:");
            String id = sc.next();
            int index = getIndex(students, id);
            if(index >= 0){
                System.out.println("请输入学生姓名:");
                String name = sc.next();
                System.out.println("请输入学生年龄:");
                int age = sc.nextInt();
                System.out.println("请输入学生地址:");
                String address = sc.next();
                students.set(index, new Student(id, name, age, address));
                System.out.println("修改成功");
                queryStudent(students);
                break;
            }else {
                System.out.println("学号不存在，请重新输入");
            }
        }
    }

    public static void queryStudent(ArrayList<Student> students) {
        System.out.println("id\t姓名\t\t年龄\t地址");
        for (Student student : students) {
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
        }
    }

    public static int getIndex(ArrayList<Student> students, String id) {
        for (int i = 0; i < students.size(); i++) {
            if(students.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    public static boolean contains(ArrayList<Student> students, String id) {
        return getIndex(students, id) >= 0;
    }
}
